/*
 * $Id: ThreadPoolTestWorker.java,v 1.2 2009/03/24 19:03:47 afaichun Exp $
 * 
 * Copyright (C) 2008 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.processor.iptds.worker.test;

import com.ge.healthcare.autosc.processor.iptds.worker.IPTDSThreadPoolManager;
import com.ge.healthcare.autosc.processor.iptds.worker.IPTDSWorker;
/**
 * 
 * @author 212042946
 *
 */
public class ThreadPoolTestWorker implements IPTDSWorker {

	private volatile boolean stop = false;
	private String workerName = null;
	private IPTDSThreadPoolManager manager = null;
	private int maxRound = 0;
	private long sleepTime = 500000;
	
	public ThreadPoolTestWorker(String name, IPTDSThreadPoolManager manager, int round) {
		workerName = name;
		this.manager = manager;
		this.maxRound = round;
	}
	
	public void stop() {
		System.out.println(workerName + " is being called to stop");
		stop = true;
		manager.removeWorker(this);
	}

	public void run() {
		int round = 0;
		try {
			while(!stop) {
				if((maxRound > 0) && (round >= maxRound)) {
					stop = true;
					continue;
				}
				System.out.println(workerName + " is running..");
				System.out.println(workerName + " sleeps for " +
						new Long(sleepTime).toString() + " seconds and run again.");
				try {
					Thread.sleep(sleepTime);
					if(maxRound>0)
						round++;
				} catch (InterruptedException ie) {
					System.out.println(workerName + " is interrupted");
				}
			}
		}
		finally {
			manager.removeWorker(this);
		}
	}

}
